package model.macro
{
	/**
	 * A fortress exists in every territory. The HP of a fortress must be
	 * reduced to 0 in a battle before the attacking troops can attack
	 * garrisoned troops.
	 */
	public class Fortress
	{
		/**The current number of hitpoints.*/
		public var hp:int;
		
		/**The base type for this fortress*/
		public var fortress_type:FortressType;
		
		/**A list of upgrades to the castle.*/
		public var upgrades:Vector.<FortressUpgrade>;
		
		/**
		 * Create a new fortress with the given type.
		 */
		public function Fortress(fortress_type:FortressType)
		{
			this.fortress_type = fortress_type;
			upgrades = new Vector.<FortressUpgrade>();
			hp = maxHP;
		}
		
		public function toString():String
		{
			return fortress_type.name;
		}
		
		/**
		 * Returns the maximum number of hit points this fortress can have.
		 * @return
		 */
		public function get maxHP():int
		{
			var bonus:int = 0;
			for (var i:int = 0; i < upgrades.length; i++)
			{
				bonus += upgrades[i].bonusHP;
			}
			return fortress_type.baseHp + bonus;
		}
		
		/**
		 * Returns the armor value.
		 * @return
		 */
		public function get armor():Number
		{
			var bonus:int = 0;
			for (var i:int = 0; i < upgrades.length; i++)
			{
				bonus += upgrades[i].bonus_armor;
			}
			return fortress_type.baseArmor + bonus;
		}
		
		/**
		 * Returns the number of archer slots.
		 * @return
		 */
		public function get archerSlots():int
		{
			var bonus:int = 0;
			for (var i:int = 0; i < upgrades.length; i++)
			{
				bonus += upgrades[i].bonus_archer_slots;
			}
			return fortress_type.archerSlots + bonus;
		}
		
		/**
		 * Returns the number of garrison slots.
		 * @return
		 */
		public function get garrisonSlots():int
		{
			var bonus:int = 0;
			for (var i:int = 0; i < upgrades.length; i++)
			{
				bonus += upgrades[i].bonus_garrison_slots;
			}
			return fortress_type.garrisonSlots + bonus;
		}
		
		/**
		 * Add an upgrade to the fortress.
		 * @param	upgrade
		 */
		public function addUpgrade(upgrade:FortressUpgrade):void
		{
			this.upgrades.push(upgrade);
		}	
	}
}